package com.streamsets.datacollector.pileline.stage;

import com.streamsets.datacollector.model.etl.RenameMappingConfigJson;
import com.streamsets.datacollector.restapi.bean.ConfigConfigurationJson;
import com.streamsets.datacollector.restapi.bean.StageConfigurationJson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *  初始化 管道 字段映射 Stage的配置
 *  stage的完整描述示例如：
 *  {
 *         "name" : "renameMapping",
 *         "value" : [ {
 *           "fromFieldExpression" : "/role_type",
 *           "toFieldExpression" : "/role_type_name"
 *         }, {
 *           "fromFieldExpression" : "/role_name",
 *           "toFieldExpression" : "/role_name_new"
 *         } ]
 *  }
 *
 * @author wengzhonghui
 * @date 2020/4/2 10:32
 */
public class InitRenameMappingConfig {

    /*
     * stage instanceName名称
     */
    private static final String STAGE_NAME = "g1_02";

    /*
     * 配置实例 名称
     */
    private static final String RENAME_MAPPING_CONFIG_NAME = "renameMapping";


    public static boolean init(StageConfigurationJson stage, RenameMappingConfigJson configDto){
        if(STAGE_NAME.equalsIgnoreCase(stage.getInstanceName())) {
            if (configDto != null) {
                List<ConfigConfigurationJson> configs = intConfig(stage.getConfiguration(), configDto);
                stage.setConfiguration(configs);
            } else {
                List<ConfigConfigurationJson> configs =
                    stage.getConfiguration().stream().filter(e -> "renameMapping".equals(e.getName())).collect(Collectors
                        .toList());
                stage.getConfiguration().removeAll(configs);
            }
        }
        return true;
    }

    public static List<ConfigConfigurationJson> intConfig(List<ConfigConfigurationJson> oldConfigs, RenameMappingConfigJson configDto){
        List<ConfigConfigurationJson> newConfigs = new ArrayList<>();
        for(ConfigConfigurationJson config :oldConfigs ){
            config = initNameMappingConfigs(config, configDto.getNameMappingColumns());
            newConfigs.add(config);
        }
        return newConfigs;
    }



    /*
     * 把当值为空时，替换为某个值
     * 格式示例如：
     * [ {
          "fromFieldExpression" : "/role_type",
          "toFieldExpression" : "/role_type_name"
        }, {
          "fromFieldExpression" : "/role_name",
          "toFieldExpression" : "/role_name_new"
        } ]
     * @param [config 原来的配置, columnNames:需要映射的列名列表]
     * @return 返回新生成的配置
     */
    private static ConfigConfigurationJson initNameMappingConfigs(ConfigConfigurationJson config, List<Map<String, String>> nameMappingColumns){
        if(RENAME_MAPPING_CONFIG_NAME.equalsIgnoreCase(config.getName())){
            List configValList = new ArrayList();
            if(nameMappingColumns != null && nameMappingColumns.size() > 0){
                for(Map<String, String> valConfig : nameMappingColumns){
                    Map data = new HashMap();
                    data.put("fromFieldExpression", "/" + valConfig.get("fromFieldExpression"));
                    data.put("toFieldExpression", "/" + valConfig.get("toFieldExpression"));
                    configValList.add(data);
                }
            }
            config = new ConfigConfigurationJson(RENAME_MAPPING_CONFIG_NAME  ,configValList);
        }
        return config;
    }



}
